//
//  MainViewController.swift
//  Weibo
//
//  Created by 李东 on 15/12/16.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //实例化一个MianTabBar
    let mainTabBar = MainTabBar()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //tabbar是一个只读属性不能直接进行设置,可以应用kvc在运行时间接赋值(改变其类型)
        print(tabBar.classForCoder)//打印类型
        //利用kvc进行复制
        setValue(mainTabBar, forKey: "tabBar")
        print(tabBar.classForCoder)
        //添加按钮点击事件
        mainTabBar.composeBtn.addTarget(self, action: "composeBtnClick", forControlEvents: .TouchUpInside)
        
        //添加子控制器
        addChildViewControllers()
    }
    //中间按钮的点击事件
    @objc private func composeBtnClick() {
        
        print("发送微博的点击事件")
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //添加子控制器器的方法
    private func addChildViewControllers(){
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(DiscoverTableViewController(), title: "信息", imageName: "tabbar_message_center")
        addChildViewController(MessageTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    //添加子控制器的方法
    private func addChildViewController(viewController:UIViewController, title: String, imageName: String){
        //实例化navigation控制器
        let nav = UINavigationController(rootViewController: viewController)
        //设置标题
        viewController.title = title
        //设置tabbar的图片
        viewController.tabBarItem.image = UIImage(named: imageName)
        //设置tabbar选中状态的图片
        viewController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        //将nav添加带tabbar上
        self.addChildViewController(nav)
        
    }

}
